package com.aaa.mq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

    @Bean
    public RabbitTemplate initRabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        // 编码格式
        rabbitTemplate.setEncoding("UTF-8");
        rabbitTemplate.setConnectionFactory(connectionFactory);
        rabbitTemplate.setMandatory(true);

        // 发布整个：确认回调
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                /*System.out.println(correlationData);
                System.out.println("确认情况：" + b);
                System.out.println("消息：" + s);*/
            }
        });

        // 返回回调:发送失败
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                /*System.out.println("消息参数：" + message);
                System.out.println("状态码：" + i);
                System.out.println("消息：" + s);
                System.out.println("交换机：" + s1);
                System.out.println("队列|路由：" + s2);*/
            }
        });
        return rabbitTemplate;
    }

}